//
//  HMBrokenLine.m
//  ReadDemo
//
//  Created by YPShao on 2017/11/6.
//  Copyright © 2017年 YPShao. All rights reserved.
//

#import "HMBrokenLine.h"

@interface HMBrokenLine()

@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@end

@implementation HMBrokenLine

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
       
    }
    return self;
}

- (instancetype)init
{
    if (self = [super initWithFrame:CGRectZero]) {
        
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
     [self.layer addSublayer:[self dashLine]];
}

- (CALayer *)dashLine
{
    CGRect frame =  CGRectMake(0, self.frame.size.height / 2.0, self.frame.size.width, self.frame.size.height);
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(frame.size.width, 0)];

    CAShapeLayer *dashLayer = [CAShapeLayer layer];
    self.shapeLayer = dashLayer;
    dashLayer.frame = frame;
    dashLayer.strokeColor = [UIColor blackColor].CGColor;
    dashLayer.lineDashPhase = 0;
    dashLayer.lineDashPattern = @[@5, @3];
    dashLayer.path = path.CGPath;
    return dashLayer;
}
- (void)setLineColor:(UIColor *)lineColor
{
//    [self.shapeLayer setFillColor:lineColor.CGColor];
    dispatch_async(dispatch_get_main_queue(), ^() {
        self.shapeLayer.strokeColor = lineColor.CGColor;
    });
}

@end
